package model

import (
	"pet-server/basic/config"
	"time"
)

// 地址表
type Address struct {
	Id        uint64    `gorm:"column:id;type:bigint UNSIGNED;primaryKey;not null;" json:"id"`
	CreatedAt time.Time `gorm:"column:created_at;type:datetime(3);default:NULL;" json:"created_at"`
	UpdatedAt time.Time `gorm:"column:updated_at;type:datetime(3);default:NULL;" json:"updated_at"`
	DeletedAt time.Time `gorm:"column:deleted_at;type:datetime(3);default:NULL;" json:"deleted_at"`
	Username  string    `gorm:"column:username;type:varchar(20);comment:名称;default:NULL;" json:"username"`     // 名称
	Telephone string    `gorm:"column:telephone;type:varchar(20);comment:联系电话;default:NULL;" json:"telephone"` // 联系电话
	Address   string    `gorm:"column:address;type:varchar(50);comment:地址;default:NULL;" json:"address"`       // 地址
}

func (a *Address) TableName() string {
	return "address"
}

// 地址列表
func (c *Combo) ListAddress(page, pageSize int) ([]Address, int64, error) {
	if page <= 0 {
		page = 1
	}
	if pageSize <= 0 || pageSize >= 100 {
		pageSize = 10
	}
	var total int64
	var address []Address
	config.DB.Model(&Address{}).Where("deleted_at IS NULL").Count(&total)
	err := config.DB.Where("deleted_at IS NULL").Offset((page - 1) * pageSize).Limit(pageSize).Find(address).Error
	return address, total, err

}
